<?php

namespace App\Models\Labaicai;

use App\Models\Events\Category\CategoryCreated;
use App\Models\Events\Category\CategoryCreating;
use App\Models\Events\Category\CategoryDeleted;
use App\Models\Events\Category\CategoryDeleting;
use App\Models\Events\Category\CategoryRetrieved;
use App\Models\Events\Category\CategorySaved;
use App\Models\Events\Category\CategorySaving;
use App\Models\Events\Category\CategoryUpdated;
use App\Models\Events\Category\CategoryUpdating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Labaicai\Category
 *
 * @property int $id
 * @property string $name
 * @property string $icon
 * @property float|null $recommend_in_min 推荐摄入千卡
 * @property float|null $recommend_in_max 推荐摄入千卡最高
 * @property string|null $unit
 * @property int|null $type 1食物  2运动
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Labaicai\Fooods[] $dailyFoods
 * @property-read int|null $daily_foods_count
 * @property-read mixed $test_line
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category filter()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereIcon($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereRecommendInMax($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereRecommendInMin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereUnit($value)
 * @mixin \Eloquent
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Labaicai\Category onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Labaicai\Category whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Labaicai\Category withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Labaicai\Category withoutTrashed()
 */
class Category extends Model
{
    use SoftDeletes;

    protected $dispatchesEvents = [
//        'retrieved' => CategoryRetrieved::class,
//        'creating'  => CategoryCreating::class,
//        'created'   => CategoryCreated::class,
//        'saving'    => CategorySaving::class,
//        'saved'     => CategorySaved::class,
//        'updating'  => CategoryUpdating::class,
//        'updated'   => CategoryUpdated::class,
//        'deleting'  => CategoryDeleting::class,
//        'deleted'   => CategoryDeleted::class
    ];
    protected $fillable = [
        'name',
        'icon',
        'recommend_in_min',
        'recommend_in_max',
        'unit',
        'type',
    ];
    protected $appends = ['test_line'];

    public function scopeFilter($query)
    {
        //
    }

    public function getTestLineAttribute(): bool
    {
        return $this->attributes['name'] == '早餐';
    }

    public function dailyFoods()
    {
        $data = request('date') ? request('date') : date('Y-m-d');
        return $this->belongsToMany(Fooods::class, 'daily_diet', 'category_id', 'food_id')
            ->when($data, function ($query) use ($data) {
                $query->where('created_at', $data);
            })->withPivot('number', 'consum_heat');
    }

}
